<?php
namespace App\Http\Controllers\AdminApi;

use App\Model\AdminTable;
use App\Model\ClassTable;
use App\Model\CommentSettingTable;
use App\Model\CouponListTable;
use App\Model\CouponTable;
use App\Model\UserTable;
use App\Repository\BaseController\ApiController;
use App\Repository\Common\ConstTable;
use App\Repository\Sys\JsonRespose;
use App\Repository\Sys\Str;

class CommentController extends ApiController
{


    /**
     * @api {get} /adminapi/comment/list  标签列表
     * @apiGroup Admin-Comment
     * @apiDescription  说明: 无
     *
     * @apiParam {Int} page --
     * @apiParam {Int} page_num --
     *
     * @apiSuccessExample {json} Success-Response:
     *
     * {
     * "code": 1,
     * "msg": "操作成功!",
     * "data": {
     * "count": "1",
     * "list": [
     * {
     * "id": "1",
     * "content": "222"
     * }
     * ],
     * "page": 1
     * }
     * }
     */
    public function getList()
    {
        $page = \Request::get('page', 1);
        $limit = \Request::get('page_num', 100);
        $param = [];
        $where = '1=1';
        $count = \DB::select("select count(*) as c  from comment_setting WHERE {$where}", $param)[0]->c;
        $param['offset'] = ($page - 1) * $limit;
        $param['limit'] = $limit;
        $res = \DB::select("select * from comment_setting WHERE {$where} ORDER BY id DESC limit :offset,:limit", $param);

        $list = [];
        foreach ($res as $v) {
            $list[] = $v;
        }
        $data = [
            'count' => $count,
            'list' => $list,
            'page' => $page,
        ];
        return JsonRespose::success($data);
    }

    /**
     * @api {get} /adminapi/comment/save  增加
     * @apiGroup Admin-Comment
     * @apiDescription  说明: 无
     *
     * @apiParam {String} content --
     *
     * @apiSuccessExample {json} Success-Response:
     *
     * {
    "code": 1,
    "msg": "操作成功!",
    "data": []
    }
     */
    public function postSave()
    {
        $content = \Request::get('content');

        if(!$content){
            return JsonRespose::fail();
        }
        $res = CommentSettingTable::ORM()->insertGetId([
            'content' => $content
        ]);
        if ($res) {
            return JsonRespose::success($res);
        } else {
            return JsonRespose::fail();
        }

    }
    /**
     * @api {get} /adminapi/comment/del  删除
     * @apiGroup Admin-Comment
     * @apiDescription  说明: 无
     *
     * @apiParam {Int} id --
     *
     * @apiSuccessExample {json} Success-Response:
     *
     * {
    "code": 1,
    "msg": "操作成功!",
    "data": []
    }
     */
    public function postDel()
    {
        $id = \Request::get('id');
        if(!$id){
            return JsonRespose::fail();
        }
        $res = CommentSettingTable::ORM()->where('id',$id)->delete();
        if ($res) {
            return JsonRespose::success();
        } else {
            return JsonRespose::fail();
        }
    }
}